home *** CD-ROM | disk | FTP | other *** search
/ The 640 MEG Shareware Studio 2 / The 640 Meg Shareware Studio CD-ROM Volume II (Data Express)(1993).ISO / clang / pcc12c.zip / LIFE.C < prev    next >
C/C++ Source or Header  |  1988-11-29  |  6KB  |  254 lines

  1. /*    LIFE.C        The much implemented game of Life invented by John Conway
  2.  
  3.                 This version was written to illustrate the use of the C88
  4.                 screen and keyboard interface. Use C option for color display.
  5.  
  6.                 To generate:
  7.                 A>PCC LIFE
  8.                 A>ASM88 PCIO
  9.                 A>BIND LIFE PCIO        */
  10.  
  11.  
  12. /*
  13.   global constant and data declarations
  14. */
  15.  
  16. #define    ROWS    24
  17. #define COLS    80
  18.  
  19. /* control key translations */
  20. #define up_char 30
  21. #define down_char    31
  22. #define left_char 29
  23. #define right_char 28
  24. #define bol_char 200
  25. #define eol_char 201
  26. #define pageup_char 202
  27. #define pagedown_char 203
  28. #define bof_char 204
  29. #define eof_char 205
  30. #define Ins_char 206
  31. #define Del_char 207
  32. #define NextWord_char 208
  33. #define PrevWord_char 209
  34.  
  35. /* function keys */
  36. #define M1 210
  37. #define M2 211
  38. #define M3 212
  39. #define M4 213
  40. #define M5 214
  41. #define M6 215
  42. #define M7 216
  43. #define M8 217
  44. #define M9 218
  45. #define M10 219
  46.  
  47.  
  48. char world[ROWS][COLS],create_mode=1,quit_flag;
  49. int  population,generation,crow,ccol;
  50. char color_opt,color;
  51.  
  52. main(argc,argv)
  53.     int argc;
  54.     char *argv[]; {
  55.  
  56.     if (argc > 1 && toupper(*argv[1]) == 'C') color_opt=1;
  57.     scr_setup();
  58.     scr_clr();
  59.     instruct();
  60.     setup();
  61.  
  62.     do {
  63.         generation++;
  64.         cycle();
  65.         screen();
  66.         }
  67.     while (population && !quit_flag);
  68.     scr_rowcol(ROWS,10);
  69.     if (population == 0)
  70.         puts("Nobody left, sorry about that.                       ");
  71.     else puts("bye                                                 ");
  72.     scr_curson();
  73.     }
  74.  
  75. instruct() {        /*    print instructions    */
  76.  
  77. puts("                The game of Life by John Conway\n\n");
  78. puts("                 Use LIFE C with color monitor.\n");
  79. puts("      If started with a number, a random pattern starts the game.\n\n");
  80. puts("  Otherwise, move the cursor with the four arrow keys to create life.\n\n");
  81. puts("     DEL changes cursor movement to mean that cells are deleted\n\n");
  82. puts("                 INS flips back to create mode.\n\n");
  83. puts("          The '+' key will toggle the game on or off.\n\n");
  84. puts("                     Hit ESC to bail out.\n\n");
  85. puts("            Enter starting number of cells or hit CR   ");
  86.     }
  87.  
  88. setup() {
  89.     int rnumber;
  90.     int i,row,col,seed,rnum;
  91.     char ch;
  92.  
  93.     rnumber=0;
  94.     while (1) {
  95.         while ((ch=scr_csts()) == 0) seed++;
  96.         if (ch < '0' || ch > '9') break;
  97.         scr_co(ch);
  98.         rnumber*=10;
  99.         rnumber+=ch-'0';
  100.         }
  101.     scr_cursoff();
  102.     scr_clr();
  103.     scr_rowcol(ROWS,20);    /*    print population message    */
  104.     puts("Generation    0     Population    0");
  105.  
  106.     srand(seed);        /*    initilize random number generator    */
  107.  
  108.     for (i=0; i < rnumber; i++) {
  109.         rnum=rand();
  110.         row=rnum%ROWS;
  111.         col=(rnum/ROWS)%COLS;
  112.         world[row][col]='X'; /* put in a cell */
  113.         scr_rowcol(row,col);
  114.         if (color_opt) scr_aputs("\2",++color | 0X80);
  115.         else scr_co(2);
  116.         }
  117.     if (rnumber == 0) create(1);
  118.     }
  119.  
  120.  
  121. screen() {    /* update the screen and set world back to x's    */
  122.     int  row,col;
  123.     char cell;
  124.  
  125.     population=0;
  126.     for (row=0; row < ROWS; row++) {
  127.         for (col=0; col < COLS; col++) {
  128.             cell=world[row][col];
  129.             /* stay alive if 3 neighbors, born if next to 2 or 3 */
  130.  
  131.             if (cell && (cell == 3 || cell == 'X'+2 || cell == 'X'+3)) {
  132.                 population++;
  133.                 if (cell < 'X') {
  134.                     scr_rowcol(row,col);
  135.                     if (color_opt) scr_aputs("\2",++color | 0X80);
  136.                     else scr_co(2);
  137.                     }
  138.                 cell='X';
  139.                 }
  140.             else {
  141.                 if (cell >= 'X') {
  142.                     scr_rowcol(row,col);
  143.                     scr_co(' ');
  144.                     }
  145.                 cell=0;
  146.                 }
  147.             world[row][col]=cell;
  148.             }
  149.         }
  150.     scr_rowcol(ROWS,31);
  151.     printf("%4d",generation);
  152.     scr_rowcol(ROWS,51);
  153.     printf("%4d",population);
  154.     }
  155.  
  156.  
  157. create(suspend)            /*    see if need to create or kill cells    */
  158.     char suspend; {
  159.     char ch,wait;
  160.  
  161.     while ((ch=scr_csts()) || suspend) {
  162.         switch (ch) {
  163.             case up_char:    crow=crow ? crow-1: ROWS-1;
  164.                             break;
  165.             case down_char:    crow=crow == ROWS-1 ? 0: crow+1;
  166.                             break;
  167.             case left_char:    ccol=ccol ? ccol-1: COLS-1;
  168.                             break;
  169.             case right_char:ccol=ccol == COLS-1 ? 0: ccol+1;
  170.                             break;
  171.             case bol_char:    ccol=0;
  172.                             break;
  173.             case eol_char:    ccol=COLS-1;
  174.                             break;
  175.             case '+':        suspend=!suspend;
  176.                             continue;
  177.             case Ins_char:    create_mode=1;
  178.                             continue;
  179.             case Del_char:    create_mode=0;
  180.                             continue;
  181.             case 0x1b:        quit_flag=1;    /* flag for stop */
  182.                             return;
  183.             default:        continue;
  184.             }
  185.         world[crow][ccol]= create_mode ? 'X': 0;
  186.         scr_rowcol(crow,ccol);
  187.         if (create_mode) {
  188.             if (color_opt) scr_aputs("\2",++color | 0X80);
  189.             else scr_co(2);
  190.             population++;
  191.             }
  192.         else {
  193.             wait=30;
  194.             while (wait--) {
  195.                 if (color_opt) scr_aputs("\1",++color | 0X80);
  196.                 else scr_co(1);
  197.                 scr_rowcol(crow,ccol);
  198.                 }
  199.             scr_co(' ');
  200.             }
  201.         }
  202.     }
  203.  
  204. cycle() {                /* cycle to the next generation */
  205.     int  row,col;
  206.  
  207.     create(0);
  208.     /*    take care of left and right column first    */
  209.     for (row=0; row < ROWS; row++) {
  210.         if (world[row][0] >= 'X') add8(row,0);
  211.         if (world[row][COLS-1] >= 'X') add8(row,COLS-1);
  212.         }
  213.  
  214.     /*    take care of top and bottom line    */
  215.     for (col=1; col < COLS-1;col++) {
  216.         if (world[0][col] >= 'X') add8(0,col);
  217.         if (world[ROWS-1][col] >= 'X') add8(ROWS-1,col);
  218.         }
  219.  
  220.     /*    fill in the box, ignoring border conditions    */
  221.     for (row=1; row < ROWS-1; row++) {
  222.         for (col=1; col < COLS-1; col++) {
  223.             if (world[row][col] >= 'X' ) {
  224.                 world[row-1][col-1]++;
  225.                 world[row-1][col]++;
  226.                 world[row-1][col+1]++;
  227.                 world[row][col-1]++;
  228.                 world[row][col+1]++;
  229.                 world[row+1][col-1]++;
  230.                 world[row+1][col]++;
  231.                 world[row+1][col+1]++;
  232.                 }
  233.             }
  234.         }
  235.     }
  236.  
  237.  
  238. add8(row,col)
  239.     int  row,col; {
  240.     int  rrow,ccol,rr,cc;
  241.  
  242.     for (rr=row-1; rr <= row+1; rr++) {
  243.         for (cc=col-1; cc <= col+1; cc++) {
  244.             rrow=rr != -1 ? rr : ROWS-1;
  245.             ccol=cc != -1 ? cc : COLS-1;
  246.             if (rrow >= ROWS) rrow=0;
  247.             if (ccol >= COLS) ccol=0;
  248.             world[rrow][ccol]++;
  249.             }
  250.         }
  251.     world[row][col]--;
  252.     }
  253.     
  254.